package com.fenghongzhang.yuekao;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

public class WelcomeActivity extends AppCompatActivity {
    private ViewPager vp;

    private  List<Fragment> list;
    private LinearLayout ll;

    private TwoFragment twoFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);


        ll = (LinearLayout) findViewById(R.id.ll);
        vp = (ViewPager) findViewById(R.id.vp);


        SharedPreferences welcome = getSharedPreferences("welcome", MODE_PRIVATE);
        boolean isOpen = welcome.getBoolean("isOpen", false);
        if(isOpen){
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
            finish();
            return;
        }

        SharedPreferences.Editor edit = welcome.edit();
        edit.putBoolean("isOpen",true);
        edit.commit();


        list = new ArrayList<>();
        list.add(new OneFragment());
        twoFragment = new TwoFragment();
        list.add(twoFragment);


        final List<ImageView> imageViewList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            ImageView imageView = new ImageView(this);
            if(i == 0){
                imageView.setImageResource(R.drawable.circle_t);
            }else{
                imageView.setImageResource(R.drawable.cricle_f);
            }
            imageViewList.add(imageView);

            ll.addView(imageView);

        }

        VpAdatper vpAdatper = new VpAdatper(getSupportFragmentManager(), list);
        vp.setAdapter(vpAdatper);


        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            //当前是第几页
            @Override
            public void onPageSelected(int position) {
                //如果是最好一页,就开始倒计时
                if(position == list.size() - 1){
                    //喊一下fg.开始倒计时
                    twoFragment.handler.sendEmptyMessage(110);
                }

                for (int i = 0; i < imageViewList.size(); i++) {
                    ImageView imageView = imageViewList.get(i);
                    if(i == position){
                        imageView.setImageResource(R.drawable.circle_t);
                    }else{
                        imageView.setImageResource(R.drawable.cricle_f);
                    }
                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }
}
